﻿using MovieTheaterApp.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace MovieTheaterApp.Db
{
    public static class Seeder
    {
        public static void Seed(
            ApplicationDbContext db,
            bool seedUsers = true,
            bool seedMovies = true,
            bool seedReviews = true,
            bool seedActors = true,
            bool seedActorMovies = true
            )
        {
            List<ApplicationUser> users = new List<ApplicationUser>()
                {
                    new ApplicationUser(){ FirstName="John Doe", UserName="JohnDoe" },
                    new ApplicationUser(){ FirstName="Jane Doe", UserName="JaneDoe" },
                    new ApplicationUser(){ FirstName="Timmy Doe", UserName="TimmyDoe" }
                };

            if (seedUsers)
            {
                
                var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
                roleManager.Create(new IdentityRole { Name = "Admin" });
                using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db)))
                {
                    userManager.Create(users[0], "John123");
                    users[0] = userManager.FindByName(users[0].UserName);
                    userManager.Create(users[1], "Jane123");
                    userManager.Create(users[2], "Tim123");
                    db.SaveChanges();
                    userManager.AddToRole(users[0].Id, "Admin");
                }
               // db.Roles.Add();
               // db.SaveChanges();
                
            }

            if (seedMovies)
            {
                //seed movies
                db.Movies.AddOrUpdate(
             p => p.Title,
             new Movie { Title = "Guardians of the Galaxy", Poster = "http://media.aintitcool.com/media/uploads/2014/nordling/guardians.jpg", Rating = Rating.PG_13, ApplicationUserId = users[0].Id },
             new Movie { Title = "Dark Knight", Poster = "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX640_SY720_.jpg", Rating = Rating.PG_13, ApplicationUserId = users[0].Id },
             new Movie { Title = "X-Men days of future past", Poster = "http://slotsmarvel.com/wp-content/uploads/2014/06/x-men-days-of-future-past-professor-x.jpg", Rating = Rating.PG_13, ApplicationUserId = users[0].Id },
             new Movie { Title = "Limitless", Poster = "http://www.impawards.com/2011/posters/limitless_ver4_xlg.jpg", Rating = Rating.PG_13, ApplicationUserId = users[0].Id }
           );
                db.SaveChanges();

            }
            if (seedReviews)
            {
                db.Reviews.AddOrUpdate(
             p => p.Title,
             new Review { Title = "Meets my expectations", Comment = "Soo..exciting!", MovieId = 1 },
             new Review { Title = "Good", Comment = "It was good...left me hanging.", MovieId = 2 },
             new Review { Title = "Love It!", Comment = "This movie rocks!", MovieId = 3 }
           );
                db.SaveChanges();
            }
            if (seedActors)
            {
                db.Actors.AddOrUpdate(
                    p => p.Name,
                    new Actor { Name = "Christian Bale" },
                    new Actor { Name = "Bradley Cooper" },
                    new Actor { Name = "Vin Diesel" }
                    );
                db.SaveChanges();
            }
            if (seedActorMovies)
            {
                db.ActorMovies.AddOrUpdate(
                    p => new { p.ActorId, p.MovieId },
                    new ActorMovie { ActorId = 1, MovieId = 2 },
                    new ActorMovie { ActorId = 2, MovieId = 1 },
                    new ActorMovie { ActorId = 2, MovieId = 4 },
                    new ActorMovie { ActorId = 3, MovieId = 1 }
                    );
                db.SaveChanges();
            }

        }
    }
}
